/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package personal.recommender.dbi;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import personal.recommender.objects.Parent;
import personal.recommender.objects.Video;

/**
 *
 * @author Aya
 */
public class LTVI extends VBI{
    
    private final EntityManager em;
    
    public LTVI(){
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("RecommenderPU");
        em = factory.createEntityManager();   
    }
    
    @Override
    public Video getVideo(String identifier) throws Exception{
        Query q = em.createNativeQuery("SELECT * FROM vb.get_video(?1)", Video.class);
        q.setParameter(1, Integer.parseInt(identifier.replace("Video", "")));
        List<Video> v = q.getResultList();
        if(v != null && !v.isEmpty())
            return v.get(0);
        else
            throw new Exception("Entity not found");
    }

    @Override
    public List<Video> getAllVideos() {
        Query q = em.createNativeQuery("SELECT * FROM vb.get_all_videos()", Video.class);
        return q.getResultList();
    }

    @Override
    public List<Parent> getAnnotations(int id) {
        Query q = em.createNativeQuery("SELECT * FROM vb.get_video_annotations(?1)", Parent.class);
        q.setParameter(1, id);
        return q.getResultList();
    }

    @Override
    public List<Parent> getAnnotations(String identifier) {
        return getAnnotations(Integer.parseInt(identifier.replace("Video", "")));
    }

    @Override
    public List<Video> getVideosWithTopic(String topic) {
        Query q = em.createNativeQuery("SELECT * FROM vb.get_videos_with_topic(?1)", Video.class);
        q.setParameter(1, topic);
        return q.getResultList();
    }
    
}
